package server;

import java.io.OutputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProccesor1 implements Runnable {

    private Socket accept;
    private Map<String, HttpServlet> servletMap;

    public Socket getAccept() {
        return accept;
    }

    public void setAccept(Socket accept) {
        this.accept = accept;
    }

    public Map<String, HttpServlet> getServletMap() {
        return servletMap;
    }

    public void setServletMap(Map<String, HttpServlet> servletMap) {
        this.servletMap = servletMap;
    }

    public RequestProccesor1() {
    }

    public RequestProccesor1(Socket accept, Map<String, HttpServlet> httpServletMap) {
        this.accept = accept;
        this.servletMap = httpServletMap;
    }

    @Override
    public void run()  {
        try {
            OutputStream outputStream = accept.getOutputStream();

            // 封装Request对象和Response对象
            Request request = new Request(accept.getInputStream());
            Response response = new Response(accept.getOutputStream());

            if(servletMap.get(request.getUrl()) == null) {
                response.outputHtml(request.getUrl());
            } else {
                servletMap.get(request.getUrl()).service(request, response);
            }
            accept.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
